package com.jie.common.result;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serializable;

/**
 * @author jie
 * @version 1.0 2025-02-10
 */
@Data
@Schema(title="Result", description ="数据响应规范结构")
public class Result<T> implements Serializable {

    @Schema(name ="code", type ="Integer", description ="响应码 0:成功 其余:失败")
    private Integer code;

    @Schema(name ="message", type ="String", description ="错误信息")
    private String message;

    @Schema(name ="data", description ="响应的具体数据")
    private T data;

    public static <T> Result<T> error() {
        Result<T> result = new Result<>();
        result.code = 500;
        result.message = "系统异常";
        return result;
    }

    public static <T> Result<T> error(String msg, Integer code) {
        Result<T> result = new Result<>();
        result.code = code;
        result.message = msg;
        return result;
    }

    public static <T> Result<T> success() {
        Result<T> result = new Result<>();
        result.code = 200;
        result.message = "响应成功";
        result.data = null;
        return result;
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.code = 200;
        result.message = "响应成功";
        result.data = data;
        return result;
    }
}
